const db = require('../db')

exports.get = (req, res) => {
  const sql = 'select * from user'
  db.query(sql, (err, data) => {
    if (err) {
      return res.send('错误：' + err.message)
    }
    res.send(data)
  })
}
// 添加更新用户数据的接口
exports.update = (req, res) => {
  const id = req.query.id
  const time = req.query.time
  const txt = req.query.txt
  // console.log(req.query.id)
  // 验证请求数据
  if (!id || !time || !txt) {
    return res.status(400).send({ status: 400, message: '时间和内容不能为空' })
  }

  const sql = 'UPDATE user SET time = ?, txt = ? WHERE id = ?'
  db.query(sql, [time, txt, id], (err, result) => {
    if (err) {
      console.error('更新失败:', err)
      return res.status(500).send({ status: 500, message: '更新失败', error: err.message })
    }

    if (result.affectedRows === 0) {
      return res.status(404).send({ status: 404, message: '未找到用户数据' })
    }

    res.send({ status: 200, message: '更新成功' })
  })
}
